package com.mybatisplus.sunpineshop.works.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisplus.common.core.Result;
import com.mybatisplus.sunpineshop.works.entity.SWorks;
import com.mybatisplus.sunpineshop.works.entity.SWorksLike;
import com.mybatisplus.sunpineshop.works.service.SWorksLikeService;
import com.mybatisplus.sunpineshop.works.service.SWorksService;
import com.mybatisplus.sunpineshop.works.service.impl.SWorksLikeServiceImpl;
import com.mybatisplus.sunpineshop.works.service.impl.SWorksServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  作品
 * </p>
 *
 * @author jobob
 * @since 2020-12-29
 */
@RestController
@RequestMapping("/works")
@CrossOrigin
public class WorksApiController {

    @Autowired
    SWorksService sWorksService;

    @Autowired
    SWorksLikeService sWorksLikeService;

    /**
     * @Author GongXl
     * @Description 获得轮播图集数据
     * @Date 2021/2/18 11:31
     * @Param []
     * @return java.util.List<java.util.Map>
     *
     **/
    @RequestMapping("/swiperImgList")
    @Scheduled(cron = "0 0 1 ? * L")//每周周日凌晨一点更新
    public Result swiperImgList(){
        List<Map> sWorksList = sWorksService.randomSelect();
        if (sWorksList != null){
            return Result.success("获得记录",sWorksList);

        }
        return Result.error("没有记录");
    }


    /**
     * @Author GongXl
     * @Description 获得作品集列表数据
     * @Date 2021/2/18 11:32
     * @Param [pageindex] 获得第几页的数据
     * @return java.util.List<java.util.Map>
     *     返回值格式如：
     *     [
     *              {
     * 						id:1,
     * 						img_url:"../../static/imgs/ruoyi_index.png",
     * 						today_zan:10,
     * 						before_zan:8,
     * 						title:"若依Vue前后端分离项目"
     *                }, {
     *    					id:2,
     *    					img_url:"../../static/imgs/ruoyi_index.png",
     *    					today_zan:10,
     *    					before_zan:8,
     *    					title:"若依Vue前后端分离项目"
     *                 }
     *     ]
     **/
    @RequestMapping("/getHotWorksList")
    public Result getHotWorksList(){
        List<Map> sWorksLikeList = sWorksLikeService.getHotWorksList();
        if (sWorksLikeList != null){
            return Result.success("获得记录",sWorksLikeList);

        }
        return Result.error("没有记录");
    }


    /**
     * 分页并查询综合所有作品
     * @param
     * @param limit
     * @return
     */
    @RequestMapping("/getAllWorksList")
    public Result getAllWorksList(@RequestParam(defaultValue = "1") int pageindex,
                                  @RequestParam(defaultValue = "10") int limit){
        PageHelper.startPage(pageindex,limit);
        QueryWrapper<SWorks> queryWrapper = new QueryWrapper<SWorks>();
//        queryWrapper.orderByDesc("update_time");
        queryWrapper.orderByDesc("likenum","browsenum");
        List<Map> sWorksList = sWorksService.findAll(queryWrapper);
        PageInfo<Map> pageInfo = new PageInfo<Map>(sWorksList);
        if (pageInfo != null){
            return Result.success("获得记录",pageInfo);

        }
        return Result.error("没有记录");
    }

    /**
     * 分页并按照好评查询作品
     * @param pageindex
     * @param limit
     * @return
     */
    @RequestMapping("/getLikeWorksList")
    public Result getLikeWorksList(@RequestParam(defaultValue = "1") int pageindex,
                                   @RequestParam(defaultValue = "10") int limit){
        PageHelper.startPage(pageindex,limit);
        QueryWrapper<SWorks> queryWrapper = new QueryWrapper<SWorks>();
        queryWrapper.orderByDesc("likenum");
        PageInfo<Map> pageInfo = new PageInfo<Map>(sWorksService.findAll(queryWrapper));
        if (pageInfo != null){
            return Result.success("获得记录",pageInfo);

        }
        return Result.error("没有记录");
    }


    /**
     * 分页并按照浏览量查询作品
     * @param pageindex
     * @param limit
     * @return
     */
    @RequestMapping("/getBrowseWorksList")
    public Result getBrowseWorksList(@RequestParam(defaultValue = "1") int pageindex,
                                   @RequestParam(defaultValue = "10") int limit){
        PageHelper.startPage(pageindex,limit);
        QueryWrapper<SWorks> queryWrapper = new QueryWrapper<SWorks>();
        queryWrapper.orderByDesc("browsenum");
        PageInfo<Map> pageInfo = new PageInfo<Map>(sWorksService.findAll(queryWrapper));
        if (pageInfo != null){
            return Result.success("获得记录",pageInfo);

        }
        return Result.error("没有记录");
    }



    /**
     * @Author GongXl
     * @Description 获得作品详情
     * @Date 2021/2/22 13:32
     * @Param [pageindex]
     * @return java.util.List<java.util.Map>
     **/
    @RequestMapping("/getWorkDetail")
    public List<Map> getWorkDetail(int pageindex){
        return null;
    }
    /**
     * @Author GongXl
     * @Description 作品详情轮播图地址
     * @Date 2021/2/22 13:49
     * @Param [id]
     * @return java.util.List<java.util.Map>
     **/
    @RequestMapping("/workDetailSwiperImgList/{id}")
    public List<Map> workDetailSwiperImgList(@PathVariable(name="id") String id){
        List<Map>  list = new ArrayList<Map>();
        Map img1 = new HashMap();
        img1.put("id","1");
        img1.put("url","");
        img1.put("img","/static/imgs/ruoyi_index.png");
        list.add(img1);
        Map img2 = new HashMap();
        img2.put("id","2");
        img2.put("url","");
        img2.put("img","/static/icon/cart.png");
        list.add(img2);
        Map img3 = new HashMap();
        img3.put("id","3");
        img3.put("url","");
        img3.put("img","/static/icon/home-active.png");
        list.add(img3);
        return  list;
    }
    /**
     * @Author GongXl
     * @Description 获得作品详情基本信息
     * @Date 2021/2/22 15:30
     * @Param [id]
     * @return java.util.List<java.util.Map>
     **/
    @RequestMapping("/getWorkDetailInfo/{id}")
    public List<Map> getWorkDetailInfo(@PathVariable(name="id") String id){
        List list = new ArrayList();
        Map img1 = new HashMap();
        img1.put("id","1");
        img1.put("thumbsUp",100);//点赞数
        img1.put("title","若依前后端Vue分离项目");
        list.add(img1);
        return list;
    }
    /**
     * @Author GongXl
     * @Description 获得详情信息
     * @Date 2021/2/22 16:07
     * @Param [id]
     * @return java.util.List<java.util.Map>
     **/
    @RequestMapping("/getWorkDetailContent/{id}")
    public List<Map> getWorkDetailContent(@PathVariable(name="id") String id){
        List list = new ArrayList();
        Map img1 = new HashMap();
        img1.put("id","1");
        img1.put("status",200);//点赞数
        img1.put("content","RuoYi-Vue <img src='/static/imgs/ruoyi_index.png'/>是一个 Java EE 企业级快速开发平台，基于经典技术组合（Spring Boot、Spring Security、MyBatis、Jwt、Vue），内置模块如：部门管理、角色用户、菜单及按钮授权、数据权限、系统参数、日志管理、代码生成等。在线定时任务配置；支持集群，支持多数据源，支持分布式事务。");//富文本
        list.add(img1);
        return list;
    }
}

